package com.example.repository;

import com.example.entity.Article;
import com.example.entity.Comment;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface ArticleRepository {


    @Select("select * from article")
    @Results(id = "article",value = {
            @Result(property = "id",column = "id",jdbcType = JdbcType.INTEGER,id = true),
            @Result(property = "articleName",column = "article_name",jdbcType = JdbcType.VARCHAR)}
    )
    public List<Article> findAll();

    //多对一子查询
    @Results(id = "articleMap",value = {
            @Result(property = "id",column = "id",jdbcType = JdbcType.INTEGER,id = true),
            @Result(property = "articleName",column = "article_name",jdbcType = JdbcType.VARCHAR)
    })
    @Select("select * from article where id=#{id}")
    public Article findArticleById(int id);

    //一对多
    @Results(value = {
            @Result(property = "id",column = "id",jdbcType = JdbcType.INTEGER,id = true),
            @Result(property = "articleName",column = "article_name",jdbcType = JdbcType.VARCHAR),
            @Result(property = "comments", column = "id",javaType = List.class,many = @Many(select = "com.example.repository.CommentRepository.findByArticleId"))
    })
    @Select("select * from article where id=#{id}")
    public List<Article> findCommentByArticle(@Param("id") int id);

    //多对多
    @Select("select * from article")
    @Results(id = "LabelMap",value = {
        @Result(property = "id",column = "id",jdbcType = JdbcType.INTEGER,id = true),
        @Result(property = "articleName",column = "article_name",jdbcType = JdbcType.VARCHAR),
        @Result(property = "labels", column = "id",javaType = List.class,many = @Many(select = "com.example.repository.LabelRepository.findByLId"))
    })
    public List<Article> findArticleByLabel();
}
